/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webaccess.retailws.Util;

import com.webaccess.retailws.Bean.ProductBean;
import com.webaccess.retailws.Delegate.ProductDelegate;
import org.apache.struts.upload.FormFile;

/**
 *
 * @author Owner
 */
public class ProductUtil {

    CommonUtil commUtil = null;
    ProductBean prodBean = null;
    ProductDelegate prodDelegate = null;

    public ProductUtil() {
        commUtil = new CommonUtil();
    }

    public String uploadFile(FormFile productImage, String filePath) {
        return commUtil.uploadFile(productImage, filePath);
    }

    public String saveProduct(Long productId, String productName, String productTitle, String productDescription, String productDiscount, String productPrice,
            Long productBrandId, Long productCategoryId, String productSmallImageName, String productLargeImageName, Long productStatusId,
            String productQty, String productTaxValue, int productTaxable) {
        prodDelegate = new ProductDelegate();
        String SUCCESS = "";
        try {
            prodBean = new ProductBean();
            prodBean.setProductId(productId);

            prodBean.setProductBrandId(productBrandId);
            prodBean.setProductCategoryId(productCategoryId);
            prodBean.setProductDescription(productDescription);
            prodBean.setProductDiscount(productDiscount);
            prodBean.setProductLargeImageName(productLargeImageName);
            prodBean.setProductName(productName);
            prodBean.setProductPrice(productPrice);
            prodBean.setProductQty(productQty);
            prodBean.setProductSmallImageName(productSmallImageName);
            prodBean.setProductStatusId(productStatusId);
            prodBean.setProductTitle(productTitle);
            prodBean.setProductTaxValue(productTaxValue);
            prodBean.setProductTaxable(productTaxable);
            boolean isProductSaved = prodDelegate.saveProduct(prodBean);
            if (productId == null) {
                if (isProductSaved == false) {
                    SUCCESS = "saveproductfailure";
                } else {
                    SUCCESS = "productsuccess";
                }
            } else {
                if (isProductSaved == false) {
                    SUCCESS = "updateproductfailure";
                } else {
                    SUCCESS = "productsuccess";
                }
            }
        } catch (Exception ex) {
            if (productId == null) {
                SUCCESS = "saveproductfailure";
            } else {
                SUCCESS = "updateproductfailure";
            }
            ex.printStackTrace();
        } finally {
            return SUCCESS;
        }

    }

    public Long addToCart(Long cartId, Long productId, String addToCartQty, Long userId, String addToCartProductDate, Long cartProductCost,
            Long cartCost) {
        prodDelegate = new ProductDelegate();
        try {
            prodBean = new ProductBean();
            prodBean.setAddToCartQty(addToCartQty);
            prodBean.setProductId(productId);
            prodBean.setCartId(cartId);
            prodBean.setAddToCartProductDate(addToCartProductDate);
            prodBean.setCartProductCost(String.valueOf(cartProductCost));
            prodBean.setCartCost(String.valueOf(cartCost));
            if (prodBean.getCartId() == 0) {
                prodBean = prodDelegate.addToCart(prodBean, userId);
            } else {
                prodBean = prodDelegate.addToCart(prodBean, userId);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (cartId == 0) {
                return prodBean.getCartId();
            } else {
                return prodBean.getCartProductId();
            }
        }
    }

    public Long addProductToCart(Long cartId, Long productId, String addToCartQty, Long userId, String addToCartProductDate, Long cartProductCost,
            Long cartCost) {
        Long cartProductId = 0L;
        try {
            cartProductId = addToCart(cartId, productId, addToCartQty, userId, addToCartProductDate, cartProductCost, cartCost);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return cartProductId;
        }
    }

    public boolean updateCartProduct(Long cartId, Long productId, String addToCartQty, String addToCartProductDate, Long cartProductCost, Long cartCost) {
        prodDelegate = new ProductDelegate();
        boolean isCartProductUpdated = false;
        try {
            prodBean = new ProductBean();
            prodBean.setAddToCartQty(addToCartQty);
            prodBean.setProductId(productId);
            prodBean.setCartId(cartId);
            prodBean.setAddToCartProductDate(addToCartProductDate);
            prodBean.setCartProductCost(String.valueOf(cartProductCost));
            prodBean.setCartCost(String.valueOf(cartCost));
            isCartProductUpdated = prodDelegate.updateCartProduct(prodBean);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return isCartProductUpdated;
        }
    }

    public boolean updateCartProductQty(Long cartProductId, String cartProductQty) {
        prodDelegate = new ProductDelegate();
        boolean isCartProductQtyUpdated = false;
        try{
            prodBean = new ProductBean();
            prodBean.setCartProductId(cartProductId);
            prodBean.setAddToCartQty(cartProductQty);
            isCartProductQtyUpdated = prodDelegate.updateCartProduct(prodBean);
        }catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return isCartProductQtyUpdated;
        }
    }
}
